import Cookies from 'js-cookie'

const state = {
	reqNum: 0,
	isLoading: false,
	sidebar: {
		isUnfold: Cookies.get('sidebarStatus') ? Cookies.get('sidebarStatus') : true,
		sidebarType: 0,
		silderIndex:'',
	}
}
const mutations = {
	TOGGLE_SIDEBAR: state => {
		state.sidebar.isUnfold = !state.sidebar.isUnfold
		state.sidebar.withoutAnimation = false
		if (state.sidebar.isUnfold) {
			Cookies.set('sidebarStatus', 1)
		} else {
			Cookies.set('sidebarStatus', 0)
		}
	},
	TOGGLE_TYPE: state => {
		state.sidebar.sidebarType = 1
	},
	TOGGLE_INDEX: (state,data) => {
		state.sidebar.silderIndex = data
	},
	FOLD_SIDEBAR: (state) => {
		Cookies.set('sidebarStatus', 0)
		state.sidebar.isUnfold = false
	},
	SET_REQ_NUM: (state, data) => {
		state.reqNum += data
	},
	SET_LOAD: (state, data) => {
		state.loadingshow = data
	},
	SHOW_GLOBAL_LOADING: (state, loading) => {
		state.isLoading = loading
	},
	PUSH_TAGS(state, val) {
	  let result = state.tags.findIndex(item => item.name === val.name)
	  result === -1 ? state.tags.push(val) : ''
	},
	//关闭标签
	CLOSE_TAB(state, val) {
	  let result = state.tags.findIndex(item => item.name === val.name)
	  state.tags.splice(result, 1)
	},
	//关闭所有tagsview标签
	CLEAR_TAGS(state, val) {
	  //清空数组
	  state.tags = []
	  //跳转到首页,val接受传过来的当前路由
	  if (val !== "/index") {
	    router.push({
	      path: "/index"
	    })
	  }
	},
	//改变tagsview显示隐藏
	CHANGE_TAGSVIEW(state) {
	  state.isCollapse = !state.isCollapse
	}
}
const actions = {
	cleartab({
		commit
	}) {
		commit('CLEAR_TAGS')
	},
	pushtab({
		commit
	}) {
		commit('PUSH_TAGS')
	},
	colsetab({
		commit
	}) {
		commit('CLOSE_TAB')
	},
	// 切换样式类型
	toggleTypeSilder({
		commit
	}) {
		commit('TOGGLE_TYPE')
	},
	// 切换样式类型
	toggleTypeIndex({commit},data) {
		commit('TOGGLE_INDEX',data)
	},	
	// 展开/收起侧边栏
	toggleSideBar({
		commit
	}) {
		commit('TOGGLE_SIDEBAR')
	},
	// 收起侧边栏
	foldSideBar({
		commit
	}) {
		commit('FOLD_SIDEBAR')
	},
	// 上传loading
	setLoading({
		commit,
		state
	}, data) {
		commit('SET_LOAD', data)
	},
	// 全局loading
	setGlobalLoading({
		commit
	}, loading) {
		commit('SHOW_GLOBAL_LOADING', loading)
	}
}
export default {
	namespaced: true,
	state,
	mutations,
	actions
}
